/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.PolygonShape;
import who.tmap.toolkit.shape.annotation.LabelsListShape;

/**
 * @author stl
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JDialogPolygonProperties extends JDialog {

	private static final String NO_LABEL_LIST ="Not assigned";
	
	private PolygonShape polygon;
	private List<LabelsListShape> labelList;
	
	private javax.swing.JPanel jContentPane = null;
	private JPanel jPanelName = null;
	private JLabel jLabelName = null;
	private JPanel jPanelButtons = null;
	private JButton jButtonOK = null;
	private JButton jButtonCancel = null;
	
	private JTextField jTextFieldPolygonName = null;
	private JCheckBox jCheckBoxPolygonNameVisible = null;
	private JCheckBox jCheckBoxPolygonSegmentsVisible = null;	

	private JLabel jLabelLabelList = null;

	private JComboBox jComboBoxLabelList = null;
	
	/**
	 * This is the default constructor
	 */
	public JDialogPolygonProperties( Frame parent, boolean modal, PolygonShape polygon, List<MapShape> shapes ) {
		super( parent, modal );
		
		this.polygon =polygon;
		
		labelList =new ArrayList<LabelsListShape>();
		if ( shapes != null ) {
			for( MapShape shape : shapes ) {
				if ( shape instanceof LabelsListShape ) {
					labelList.add( (LabelsListShape)shape );
				}
			}
		}

		initialize();
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Polygon properties");
		this.setBounds(new java.awt.Rectangle(0,0,400,200));
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo( null );
		
		jTextFieldPolygonName.setText( polygon.getName() );
		jCheckBoxPolygonNameVisible.setSelected( polygon.getLabelShape().isVisible() );
		jCheckBoxPolygonSegmentsVisible.setSelected( polygon.isSegmentsVisible() );
		
		getRootPane().setDefaultButton( getJButtonOK() );
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,5,5,5));
			jContentPane.add(getJPanelName(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getJPanelButtons(), java.awt.BorderLayout.SOUTH);
		}
		return jContentPane;
	}
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelName() {
		if (jPanelName == null) {
			jLabelLabelList = new JLabel();
			jLabelLabelList.setText("Assign to Label list");
			GridLayout gridLayout = new GridLayout(3,2,5,5);
			jLabelName = new JLabel();
			jPanelName = new JPanel();
			jLabelName.setText("Polygon's name (Join ID)");
			jPanelName.setLayout(gridLayout);
			jPanelName.add(jLabelName, null);
			jPanelName.add(getJTextFieldPolygonName(), null);
			jPanelName.add(jLabelLabelList, null);
			jPanelName.add(getJComboBoxLabelList(), null);
			jPanelName.add(getJCheckBoxPolygonSegmentsVisible(), null);			
			jPanelName.add(getJCheckBoxPolygonNameVisible(), null);
		}
		return jPanelName;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			jButtonOK.setSelected(true);
			jButtonOK.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {
					keepChanges();
				}
			});
		}
		return jButtonOK;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jTextFieldPolygonName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldPolygonName() {
		if (jTextFieldPolygonName == null) {
			jTextFieldPolygonName = new JTextField();

			jTextFieldPolygonName.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					keepChanges();				
				}
			});
		}
		return jTextFieldPolygonName;
	}
	
	/**
	 * This method initializes jCheckBoxPolygonNameVisible	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxPolygonNameVisible() {
		if (jCheckBoxPolygonNameVisible == null) {
			jCheckBoxPolygonNameVisible = new JCheckBox();
			jCheckBoxPolygonNameVisible.setText("Polygon's name visible");
		}
		return jCheckBoxPolygonNameVisible;
	}
	
	private JCheckBox getJCheckBoxPolygonSegmentsVisible() {
		if (jCheckBoxPolygonSegmentsVisible == null) {
			jCheckBoxPolygonSegmentsVisible = new JCheckBox();
			jCheckBoxPolygonSegmentsVisible.setText("Segments visible");
		}
		return jCheckBoxPolygonSegmentsVisible;
	}	
	
	private void keepChanges() {
		polygon.setName( jTextFieldPolygonName.getText() );
		polygon.getLabelShape().setVisible( getJCheckBoxPolygonNameVisible().isSelected() );
		polygon.setSegmentsVisible( getJCheckBoxPolygonSegmentsVisible().isSelected() );
		
		if ( !NO_LABEL_LIST.equals( getJComboBoxLabelList().getSelectedItem().toString() ) ) {
			polygon.getLabelShape().setLabelsList( (LabelsListShape)getJComboBoxLabelList().getSelectedItem() );
		} else {
			polygon.getLabelShape().setLabelsList( null );
		}
		
		dispose();
	}
	
	/**
	 * This method initializes jComboBoxLabelList	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxLabelList() {
		if ( jComboBoxLabelList == null ) {
			Object[] labelLists =null;
			if ( labelList != null ) {
				labelLists =new Object[ labelList.size() + 1 ];
				labelList.toArray( labelLists );
			} else {
				labelLists =new Object[ 1 ];			
			}
			
			labelLists[ labelLists.length - 1 ] =NO_LABEL_LIST;
		
			jComboBoxLabelList = new JComboBox( labelLists );
			
			if ( polygon.getLabelShape() != null && polygon.getLabelShape().getLabelsList() != null ) {
				jComboBoxLabelList.setSelectedItem( polygon.getLabelShape().getLabelsList() );
			} else {
				jComboBoxLabelList.setSelectedItem( NO_LABEL_LIST );
			}
		}
		
		return jComboBoxLabelList;
	}
	
 }  
